<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "iliketry.sms_new".
 *
 * @property integer $id
 * @property integer $task_id
 * @property string $cell
 * @property string $content
 * @property string $add_time
 * @property integer $received
 * @property integer $uid
 * @property integer $department
 * @property integer $channel
 * @property string $send_time
 * @property string $sms_company
 */
class Sms extends \yii\db\ActiveRecord
{
    #短信发送
    public static $RECEIVED_SENT = 0;
    #短信回复
    public static $RECEIVED_REPLY = 1;

    #营销通道
    public static $CHANNEL_MARKETING = 1;
    #非营销通道
    public static $CHANNEL_NON_MARKETING = 0;

    public $channelNames = ['非营销', '营销'];
    public $receiveNames = ['发送', '用户回复'];

    private $smsLog = null;
    private $smsStatus = null;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'iliketry.sms_new';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['task_id', 'received', 'uid', 'department', 'channel'], 'integer'],
            [['add_time', 'send_time'], 'safe'],
            [['cell'], 'string', 'max' => 11],
            [['content'], 'string', 'max' => 500],
            [['sms_company'], 'string', 'max' => 10],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'task_id' => '任务ID',
            'cell' => '手机号',
            'content' => '短信内容',
            'add_time' => '发送时间',
            'received' => '是否回复',
            'uid' => 'Uid',
            'department' => '部门',
            'channel' => '通道',
            'send_time' => '预发送时间',
            'sms_company' => '供应商',
        ];
    }

    /**
     * 取得通道名称
     * @return mixed
     */
    public function getChannelName()
    {
        return str($this->channelNames[$this->channel]);
    }

    /**
     * 取得接收 或者 回复 中文状态
     * @return mixed
     */
    public function getReceiveName()
    {
        return str($this->receiveNames[$this->received]);
    }

    /**
     * @inheritdoc
     * @return \services\models\SmsQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \services\models\SmsQuery(get_called_class());
    }


    public function saveErrors($describe, $sendData, $receiveData, $cell = '')
    {
        if ($this->smsLog === null) {
            $this->smsLog = new SmsLog();
        }
        $this->smsLog->comment = $describe;
        $this->smsLog->source_data = $sendData;
        $this->smsLog->result_data = $receiveData;
        $this->smsLog->cell = $cell;
        $this->smsLog->insert();

    }


    public function updateStatus($task_id, $cell, $status, $time, $company = '')
    {

        $status = strlen($status) < 20 ? $status : substr($status, 0, 20);
        $status = $status == '0' ? 'DELIVRD' : $status;

        if ($this->smsStatus === null) {
            $this->smsStatus = new SmsStatus();
        }
        $result = $this->smsStatus->insert();
        $this->smsStatus->task_id = $task_id;
        $this->smsStatus->cell = $cell;
        $this->smsStatus->status = $status;
        $this->smsStatus->sent_time = $time;
        $this->smsStatus->sms_company = $company;
        return $this->smsStatus->insert();

    }
}
